package com.clcg;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.Socket;

/**
 * 作者: LCG
 * 日期: 2020/8/4 17:29
 * 描述:
 */
public class TestInvocation implements InvocationHandler {

    private String host;

    private int port;

    private String impl;

    public TestInvocation(String host, int port, String impl) {
        this.host = host;
        this.port = port;
        this.impl = impl;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        Socket socket=new Socket(host,port);

        //发送数据
        ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(args);//传参数
        oos.writeUTF(impl);//接口实现类的全限定名
        oos.writeUTF(method.getName());//方法的名称
        oos.writeObject(method.getParameterTypes());//参数的类型


        //接收 服务端的响应结果
        //接收返回的数据
        ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());

        Object result=ois.readObject();

        //关闭Socket
        socket.close();

        return result;
    }



}
